from auditlog.models import AuditlogHistoryField
from auditlog.registry import auditlog
from django.db import models
from django.utils import timezone

from naryad.models import Spr_sdl


class Base(models.Model):
    create_at = models.DateTimeField(default=timezone.now)
    update_at = models.DateTimeField(auto_now=True)
    history = AuditlogHistoryField()

    class Meta:
        abstract = True


class Uslugi(models.Model):
    usl = models.PositiveIntegerField(blank=False, null=False, verbose_name="Услуга")
    kusl = models.CharField(blank=False, max_length=50, verbose_name="Код")
    ok_isl_p = models.CharField(
        blank=False,
        null=False,
        max_length=50,
        verbose_name="Код терфонда",
    )
    nusl = models.TextField(blank=False, null=False, verbose_name="Название услуги")
    # opl = models.PositiveIntegerField(
    #     blank=False,
    #     null=False,
    #     verbose_name="Способ оплаты",
    # )
    # uet = models.FloatField(blank=False, null=False, default=0.0, verbose_name="Ует")
    uet = models.DecimalField(max_digits=10, decimal_places=2, verbose_name="Ует")
    trf = models.DecimalField(max_digits=10, decimal_places=2, verbose_name="Стоимость")
    isp = models.PositiveIntegerField(
        blank=False,
        null=False,
        default=1,
        verbose_name="Использование",
    )
    dnu = models.DateField(blank=False, null=False, verbose_name="Дата начала действия")
    dku = models.DateField(blank=True, null=True, verbose_name="Конец действия")
    vt = models.BooleanField(default=False, verbose_name="Высокотехнологичная")

    class Meta:
        verbose_name = "Справочник услуг"
        verbose_name_plural = "Справочник услуг"

    def __str__(self) -> str:
        return f"{self.kusl}"


class Ztarifs(Base):
    usl = models.ForeignKey(Uslugi, on_delete=models.CASCADE)
    sdl = models.ForeignKey(Spr_sdl, on_delete=models.CASCADE)
    trf_zp = models.DecimalField(
        max_digits=8,
        decimal_places=2,
        verbose_name="Зарплатный тариф",
    )
    dn = models.DateField(blank=False, null=False, verbose_name="Дата начала действия")
    dk = models.DateField(blank=True, null=True, verbose_name="Дата окончания действия")
    type_usl = models.PositiveIntegerField(default=0, verbose_name="Тип услуги")

    class Meta:
        verbose_name = "Тарифы специалистов"
        verbose_name_plural = "Тарифы специалистов"

    def __str__(self) -> str:
        return f"{self.usl.kusl} - {self.sdl.name_sdl} - {self.trf_zp}"


auditlog.register(Ztarifs, serialize_data=True)
